/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.api;

import com.jeesite.modules.storage.service.MemberService;
import com.jeesite.modules.storage.common.ServerResponse;
import com.jeesite.modules.storage.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.UUID;

/**
 * 会员Controller
 */
@RestController(value = "apimember")
@RequestMapping(value = "/api/member")
public class MemberController extends APIController {

    @Autowired
    private MemberService memberService;

    /**
     * 登录
     * @param platform 平台【APP或H5】
     * @param memberId 会员Id
     * @return 会员
     */
    @RequestMapping(method = RequestMethod.POST,value = "login")
    public ServerResponse<Map> login(@RequestParam(value="platform") String platform,@RequestParam(value="memberId") String memberId)
    {
        return ServerResponse.createBySuccess(200,"获取成功",memberService.getStorageMap(platform,memberId,null,null));
    }

    /**
     * 统计汇总
     * @param memberId 会员Id
     * @return 统计汇总
     */
    @RequestMapping(method = RequestMethod.GET,value = "statistics")
    @ResponseBody
    public ServerResponse<Map> statistics(@RequestParam(value = "memberId")String memberId){
        return ServerResponse.createBySuccess(200,memberService.getStatisticsById(memberId));
    }

    /**
     * 上传
     * @param memberId 会员Id
     * @param file 会员Id
     * @return 图片
     */
    @RequestMapping(method = RequestMethod.POST,value = "avatar")
    @ResponseBody
    public ServerResponse<String> avatar(@RequestParam(value = "memberId")String memberId,MultipartFile file){
        String avatar = "/userfiles/member/avatar/"+ UUID.randomUUID() + WebUtil.getExtension(file.getOriginalFilename());
        Integer code = memberService.changeAvatar(memberId,save(file,avatar));
        return ServerResponse.createBySuccess(code, "更新成功",avatar);
    }

    /**
     * 账号余额
     * @param memberId 会员Id
     * @return 账号余额
     */
    @RequestMapping(method = RequestMethod.GET,value = "balance")
    @ResponseBody
    public ServerResponse<Double> balance(@RequestParam(value = "memberId")String memberId){
        return ServerResponse.createBySuccess(200,memberService.getBalance(memberId));
    }
}